﻿
using System;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using System.Configuration;
namespace Time.OPCServerV
{
    public class OpcBusiness
    {
        private object D11220_Value;
        private object D11420_Value;
        private object D11421_Value;
        private object D11422_Value;
        private object D11423_Value;
        private object D11424_Value;
        private object D11425_Value;
        private object D11426_Value;
        private object D11446_Value;
        private object D11447_Value;
        private object D11448_Value;
        private object D11449_Value;
        private static readonly OpcBusiness instance = new OpcBusiness();
        public List<string> lstItemIDs = new List<string>();
        public object[] NewValues = null;
        public Dictionary<string, object> dicTagValue = new Dictionary<string, object>();
        public void DisOpc()
        {
            OpcHandle instance = OpcHandle.GetInstance();
            if (instance.ItemIDs != null)
            {
                instance.DisOpc();
            }
        }

        public static OpcBusiness GetInstance()
        {
            return instance;
        }

        //public void Init()
        //{
        //    this.OpcConnect();
        //}
       
        //private Boolean OpcConnect()
        //{
        //    int n = 0;
        //    bool flag;
        //    //KanBan.初始系统所辖计算机信息();
        //    //if (!KanBan.检查计算机信息())
        //    //{
        //    //    return;
        //    //}
        //    Label_0102:
        //    flag = true;
        //    OpcHandle instance = OpcHandle.GetInstance();
        //    if (instance.ItemIDs == null)
        //    {
        //        string argDescription=null;
        //        this.lstItemIDs = new List<string>(); 
        //        string str2 = "Channel1.Device1.";
        //        this.lstItemIDs.Add(str2 + "DSH11220.20");
        //        for (int i = 0; i < 11; i++)
        //        {
        //            int num2 = 0x2c9c + i;
        //            this.lstItemIDs.Add(str2 + "D" + num2.ToString().Trim());
        //        }
        //        instance.ConnectPLC(this.lstItemIDs.ToArray(), ref argDescription);
        //        if (argDescription.Contains("成功"))
        //        {
        //            new Thread(new ThreadStart(this.ReadOpcHandle)) { IsBackground = true }.Start();
        //        }
        //        else
        //        {
        //            n++;
        //            if(n>3)
        //            {
        //                return false;
        //            } 
        //            Thread.Sleep(0x2710);
        //            goto Label_0102;
        //        }
              
        //    }
        //    return true;
        //}
        public Boolean Init(List<string> lstItem, ref string argDescription)
        {
           return  OpcConnect(lstItem,ref argDescription);
        }
        private Boolean OpcConnect(List<string> lstItem,ref string argDescription)
        {
            int i = 2; 
            try
            {
                //KanBan.初始系统所辖计算机信息();
                //if (!KanBan.检查计算机信息())
                //{
                //    return false;
                //}
                Label_0102:
               
                OpcHandle instance = OpcHandle.GetInstance();
                if (instance.ItemIDs == null)
                {
                    this.lstItemIDs = lstItem.GetRange(0, lstItem.Count);
                    instance.ConnectPLC(lstItemIDs.ToArray(), ref argDescription);
                    if (argDescription.Contains("成功"))
                    {
                        new Thread(new ThreadStart(this.ReadOpcHandle)) { IsBackground = true }.Start();
                    }
                    else
                    {
                        i++;
                        if (i > 2)
                        {
                            return false;
                        }
                        Thread.Sleep(0x2710);
                        goto Label_0102;
                    }
                }
            }
            catch (Exception ex)
            {
                argDescription = ex.Message;
                return false;
            }
            return true;
        }
        public object ReadData(string sAddress)
        {
            switch (sAddress)
            {
                case "D11220":
                    return this.D11220_Value;

                case "D11420":
                    return this.D11420_Value;

                case "D11421":
                    return this.D11421_Value;

                case "D11422":
                    return this.D11422_Value;

                case "D11423":
                    return this.D11423_Value;

                case "D11424":
                    return this.D11424_Value;

                case "D11425":
                    return this.D11425_Value;

                case "D11426":
                    return this.D11426_Value;

                case "D11446":
                    return this.D11446_Value;

                case "D11447":
                    return this.D11447_Value;

                case "D11448":
                    return this.D11448_Value;

                case "D11449":
                    return this.D11449_Value;
                default:
                   return  getDictionaryV(dicTagValue, sAddress);
            }
            
        }
        private void SetDataV(object[] objValues)
        {
            int countTag = lstItemIDs.Count;
            for (int i = 0; i < countTag; i++)
            {
                setDictionaryV(dicTagValue, lstItemIDs[i], objValues[i]);
            }
        }
        private void ReadOpcHandle()
        {
            while (true)
            {
                try
                {
                    int[] argsQualities = new int[1];
                    OpcHandle instance = OpcHandle.GetInstance();
                    object[] objValues = instance.ReadItems(instance.ItemIDs, ref argsQualities);
                    for (int i = 0; i < argsQualities.Length; i++)
                    {
                        if (argsQualities[i] != 0xc0)
                        {
                        }
                    }
                    this.NewValues = objValues;
                    //this.SetData(objValues);
                    SetDataV(objValues);
                    //if (Convert.ToInt32(objValues[8]) == 1)
                    //{
                    //}
                }
                catch
                {
                }
                Thread.Sleep(200);
            }
        }

        private void SetData(object[] objValues)
        {
            int index = -1;
            index++;
            this.D11220_Value = objValues[index];
            index++;
            this.D11420_Value = objValues[index];
            index++;
            this.D11421_Value = objValues[index];
            index++;
            this.D11422_Value = objValues[index];
            index++;
            this.D11423_Value = objValues[index];
            index++;
            this.D11424_Value = objValues[index];
            index++;
            this.D11425_Value = objValues[index];
            index++;
            this.D11426_Value = objValues[index];
            index++;
            this.D11446_Value = objValues[index];
            index++;
            this.D11447_Value = objValues[index];
            index++;
            this.D11448_Value = objValues[index];
            index++;
            this.D11449_Value = objValues[index];
        } 
        public bool WriteData(string sAddress, object argValue, ref int argError, string ChannelNo="Channel1.", string DeviceNo = "Device1.")
        {
            OpcHandle instance = OpcHandle.GetInstance();
            int num = -1;
            if (sAddress == "D11220")
            { 
                sAddress = "DSH1122020";
            }
            return instance.WriteItems(ChannelNo+ DeviceNo + sAddress, argValue, ref num);
        }
        /// <summary>
        /// 获取指定字典，指定键值
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="stringKey"></param>
        /// <returns></returns>
        private static object getDictionaryV(Dictionary<string, object> dic, string stringKey)
        {
            object stringRtn;
            if (dic.Keys.Contains(stringKey))
                stringRtn = dic[stringKey];
            else
                stringRtn = null;
            return stringRtn;
        }
        /// <summary>
        /// 设定指定字典指定键值，存在则修改，不存在则添加
        /// </summary>
        /// <param name="dic"></param>
        /// <param name="stringKey"></param>
        /// <param name="stringValue"></param>
        /// <returns></returns>
        private static void setDictionaryV(Dictionary<string, object> dic, string stringKey, object stringValue)
        {
            if (dic.Keys.Contains(stringKey))
                dic[stringKey] = stringValue;
            else
                dic.Add(stringKey, stringValue);
        }
    }
}

